package com.mini.mybatis.mapping;

import com.mini.mybatis.session.Configuration;

/**
 * 映射语句 - 封装SQL映射语句的配置信息
 */
public class MappedStatement {
    
    private String resource;
    private Configuration configuration;
    private String id;
    private SqlCommandType sqlCommandType;
    private BoundSql boundSql;
    private Class<?> resultType;
    private String parameterType;
    
    MappedStatement() {
        // 构造器私有化，通过Builder创建
    }
    
    /**
     * 获取资源路径
     */
    public String getResource() {
        return resource;
    }
    
    /**
     * 获取配置对象
     */
    public Configuration getConfiguration() {
        return configuration;
    }
    
    /**
     * 获取语句ID
     */
    public String getId() {
        return id;
    }
    
    /**
     * 获取SQL命令类型
     */
    public SqlCommandType getSqlCommandType() {
        return sqlCommandType;
    }
    
    /**
     * 获取绑定的SQL
     */
    public BoundSql getBoundSql() {
        return boundSql;
    }
    
    /**
     * 获取结果类型
     */
    public Class<?> getResultType() {
        return resultType;
    }
    
    /**
     * 获取参数类型
     */
    public String getParameterType() {
        return parameterType;
    }
    
    /**
     * MappedStatement构建器
     */
    public static class Builder {
        
        private MappedStatement mappedStatement = new MappedStatement();
        
        public Builder(Configuration configuration, String id, SqlCommandType sqlCommandType, BoundSql boundSql) {
            mappedStatement.configuration = configuration;
            mappedStatement.id = id;
            mappedStatement.sqlCommandType = sqlCommandType;
            mappedStatement.boundSql = boundSql;
        }
        
        public Builder resource(String resource) {
            mappedStatement.resource = resource;
            return this;
        }
        
        public Builder resultType(Class<?> resultType) {
            mappedStatement.resultType = resultType;
            return this;
        }
        
        public Builder parameterType(String parameterType) {
            mappedStatement.parameterType = parameterType;
            return this;
        }
        
        public MappedStatement build() {
            assert mappedStatement.configuration != null;
            assert mappedStatement.id != null;
            assert mappedStatement.sqlCommandType != null;
            assert mappedStatement.boundSql != null;
            
            return mappedStatement;
        }
    }
}